package com.sinosoft.dsp.component.bpm.viewbean.searchdata;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.pvm.internal.history.model.HistoryProcessInstanceImpl;
import org.primefaces.model.SortOrder;

import com.sinosoft.dsp.component.bpm.util.JbpmConfig;
import com.sinosoft.dsp.platform.components.portal.iteration.DataTableHeaderObject;
import com.sinosoft.dsp.platform.components.portal.iteration.ISinoLazyRowDataTableViewBean;
import com.sinosoft.dsp.platform.components.portal.iteration.RowDataTableObject;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoAbstractLazyRowDataTableViewBean;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.to.IGenericTO;

public class BpmActiveDataViewBean extends SinoAbstractLazyRowDataTableViewBean
		implements ISinoLazyRowDataTableViewBean {

	@Override
	public List<DataTableHeaderObject> initHeader(IGenericTO tos) {
		List<DataTableHeaderObject> rtn = new ArrayList<DataTableHeaderObject>();
		rtn.add(new DataTableHeaderObject("ID").setDescription("ID"));
		rtn.add(new DataTableHeaderObject("状态").setDescription("状态"));// active:运行中,ended:结束
		rtn.add(new DataTableHeaderObject("流程类型").setDescription("流程类型"));
		rtn.add(new DataTableHeaderObject("当前活动").setDescription("当前活动"));
		rtn.add(new DataTableHeaderObject("创建者").setDescription("创建者"));
		rtn.add(new DataTableHeaderObject("创建时间").setDescription("创建时间"));
		rtn.add(new DataTableHeaderObject("结束时间").setDescription("结束时间"));
		rtn.add(new DataTableHeaderObject("持续时间").setDescription("持续时间"));
		return rtn;
	}

	@Override
	public List<RowDataTableObject> fetchPage(int first, int pageSize,
			String sortField, SortOrder sortOrder, Map<String, String> filters)
			throws SinoPlatformException {
		IBpmInstanceSearchViewBean search = (IBpmInstanceSearchViewBean) this
				.getParent();
		System.out.println(search);
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		
		//jbpmConfig.getTaskService().getTask(taskId);
		
		List<HistoryProcessInstance> list = jbpmConfig.getHistoryService()
				.createHistoryProcessInstanceQuery().list();
		List<RowDataTableObject> rows = new ArrayList<RowDataTableObject>();
		if (list != null && list.size() > 0) {
			for (HistoryProcessInstance pi : list) {
				HistoryProcessInstanceImpl ei = (HistoryProcessInstanceImpl) pi;
				RowDataTableObject obj = new RowDataTableObject();
				obj.addRow(String.valueOf(ei.getDbid()));
				obj.addRow("2");
				obj.addRow("3");
				obj.addRow("4");
				obj.addRow("5");
				rows.add(obj);
			}
		}
		return rows;
	}

	@Override
	public int fetchRowCount(String sortField, SortOrder sortOrder,
			Map<String, String> filters) throws SinoPlatformException {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		long row = jbpmConfig.getHistoryService()
				.createHistoryProcessInstanceQuery().count();
		return Long.valueOf(row).intValue();
	}

}
